package com.service.impl;

import com.dao.CourseDao;
import com.domain.*;
import com.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseDao courseDao;
    private int pageSize = 6;
    /**
     * 获取页面信息
     * @param currentPage
     * @param role
     * @param tid
     * @return
     */
    public PageBean getPageBean(int currentPage,int role,int tid){
        int total = courseDao.totalCourse(tid,role);    //总记录数
        int totalPage = 0;  //总页面数
        if (total % pageSize == 0) {
            totalPage = total / pageSize;
        }else {
            totalPage = total / pageSize + 1;
        }
        int start = (currentPage - 1) * pageSize;
        PageBean pageBean = new PageBean(pageSize,currentPage,totalPage,start,null);
        return pageBean;
    }
    /**
     * 查询全部课程 如果role=1时，查询教师授课的课程
     * @param name
     * @return
     */
    @Override
    public PageBean findAndSearchCourse(int currentPage,String name,int role,int tid) {
        PageBean bean = getPageBean(currentPage,role,tid);
        if (name != null) {
            bean.setList(courseDao.findAndSearchCourse("%" + name + "%", bean.getStart(), bean.getPageSize(), role, tid));
        }else {
            bean.setList(courseDao.findAndSearchCourse(null,bean.getStart(),bean.getPageSize(),role,tid));
        }
        return bean;
    }
    /**
     *  查询课程总数,分页时用
     */
    @Override
    public int totalCourse(int tid,int role) {
        return courseDao.totalCourse(tid, role);
    }
    /**
     * 查询学生课程
     * @param flag
     * @param studentID
     * @return
     */
    @Override
    public PageBean stuFindCourse(int currentPage,int flag, int studentID) {
        PageBean bean = getPageBean(currentPage,0,0);
        List<StuCourse> stuCourses = courseDao.stuFindCourse(flag, studentID, bean.getStart(), bean.getPageSize());
        bean.setList(stuCourses);
        return bean;
    }
    @Override
    public List<SelectCourse> findCourseByName(int id) {
        return courseDao.findCourseByName(id);
    }
    /**
     * 添加课程信息
     * @param course
     */
    @Override
    public String addCourse(Course course) {
        if (course.getCourseID() == null){
            return "null";
        }
        if (course.getCourseName() == null){
            return "null";
        }
        if (course.getScore() == null){
            return "null";
        }
        courseDao.addCourse(course);
        return "OK";
    }
    /**
     * 删除某课程
     * @param courseID
     */
    @Override
    public void deleteCourse(int courseID) {
        courseDao.deleteCourse(courseID);
    }
    /**
     * 通过课程号查询课程信息
     * @param courseID
     * @return
     */
    @Override
    public Course findCourseById(int courseID) {
        return courseDao.findCourseById(courseID);
    }
    /**
     * 修改课程信息
     * @param course
     */
    @Override
    public void updateCourse(Course course) {
        courseDao.updateCourse(course);
    }

}
